from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse

from jilu.models import Content
from django.db.models import F


class MD1(MiddlewareMixin):

    def process_request(self, request):
        print('---MD1----------process_request------', id(request))
        Content.objects.filter(id=1).update(count=F('count') + 1)

        # return HttpResponse('截断了')

    def process_view(self, request, view_func, view_args, view_kwargs):
        print('---MD1----------process_view------', id(request))
        # raise Exception('哈哈') 异常后,并不能
        # return HttpResponse('哈哈')

    def process_response(self, request, response):
        print('---MD1----------process_response------', id(request))
        response['ss'] = '1234'
        return response

    def process_exception(self, request, exception):
        print('---MD1----------process_exception------', id(request))

        return HttpResponse(exception)


class MD2(MiddlewareMixin):

    def process_request(self, request):
        print('---MD2----------process_request------', id(request))
        # return HttpResponse('哈哈')
        # return HttpResponse('截断了')

    def process_view(self, request, view_func, view_args, view_kwargs):
        print('---MD2----------process_view------', id(request))
        # raise Exception('哈哈') 异常后,并不能

    def process_response(self, request, response):
        print('---MD2----------process_response------', id(request))
        response['ss'] = '1234'
        return response

    def process_exception(self, request, exception):
        print('---MD2----------process_exception------', id(request))

        return HttpResponse(exception)
